﻿using Cache.Redis;
using Common;
using DataService.User;
using Model.Port;
using Model.User;
using Model.Voyage;
using Service.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace Service.User
{
    //获取角色信息
    public class ResponseGetRoleInfo : IQueryInterface
    {
        /// 处理完数据，修改r d的值 r-int;d-Dictionary<string, object>
        /// <summary>
        /// 处理完数据，修改r d的值 r-int;d-Dictionary<string, object>
        /// </summary>
        /// <param name="param">传参ParamData</param>
        /// <param name="data"></param>
        public void GetData(string uid, Dictionary<string, object> p, ref Dictionary<string, object> d)
        {
            try
            {
                if (!string.IsNullOrEmpty(uid))
                {
                    //获取角色信息
                    GetRoleByUID(uid, ref d);

                    //result["r"] = 1;
                    //result["d"] = role;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("ResponseGetRoleInfo.GetData", ex);
            }
        }

        #region 数据操作

        /// 获取角色信息
        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public void GetRoleByUID(string uid, ref Dictionary<string, object> d)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();


            TableService.GetTableObjectList<TableCommonProperty>();

            RoleData roleinfo = DataBusiness.GetValue<RoleData>(uid);

            //ShipTrainData _shiptrain_data = RedisHelper.instance.GetObjectDataByKey<ShipTrainData>(uid);

            //ShipData _ship_data = RedisHelper.instance.GetObjectDataByKey<ShipData>(uid);

            //VoyageData _voyage_data = RedisHelper.instance.GetObjectDataByKey<VoyageData>(uid);

            //SeaConnectData _sea_connect_data = RedisHelper.instance.GetObjectDataByKey<SeaConnectData>(uid);

            //SeaMaskData _sea_mask_data = RedisHelper.instance.GetObjectDataByKey<SeaMaskData>(uid);

            //SeaPointData _sea_point_data = RedisHelper.instance.GetObjectDataByKey<SeaPointData>(uid);

            SeaHelper.UpdateSeaPointPopular(uid, roleinfo);

            SeaHelper.UpdateSeaWindy(roleinfo);

            //SeaHelper.UpdateAllVoyage(uid);

            WorkLineHelper.UpdateTimeLine(uid);

            //d = ChangeToResponse(roleinfo);

            //判断修理、改造中的船舶，是否已经完成
            //ShipData ship_data = RedisHelper.instance.GetObjectDataByKey<ShipData>(uid);    //获取我的船舶信息
            //List<Ship> shipList = RedisHelper.instance.GetHashObjectList<Ship>(uid);    //获取所有船舶信息
            List<Ship> shipList = DataBusiness.GetObjectList<Ship>(uid);    //获取所有船舶信息
            DateTime dateNow = DateTime.Now;
            bool isNeedUpdate = false;
            if (shipList != null && shipList.Any())
            {
                foreach (var item in shipList)
                {
                    List<TableShipLvUp> table_ship_lv = TableService.GetTableObjListById<TableShipLvUp>(item.TableId.ToString());    //船舶等级
                    if (item.Status > 0)
                    {
                        isNeedUpdate = true;
                        DateTime endTime = CheckInfo.ConvertUTCTimeStampToDateTime(item.FixStartTime + item.FixTime);
                        if (dateNow.CompareTo(endTime) >= 0)
                        {
                            item.Level = item.Status == 2 ? item.Level + 1 : item.Level;
                            item.Status = 0;
                            item.Durability = table_ship_lv.Where(x => x.mShipLv == item.Level).FirstOrDefault().mMaxDurable;   // table_ship.mMaxDurable;
                            item.FixStartTime = 0;
                            item.FixTime = 0;
                        }
                        else
                        {
                            item.FixTime = int.Parse(Math.Ceiling((endTime - dateNow).TotalSeconds).ToString());
                        }
                    }
                }
                //RedisHelper.instance.RemoveKey<Ship>(uid);  //首先清除所有船舶信息
                //RedisHelper.instance.SaveRangeObjectInHash<Ship>(uid, shipList);   //修理、改造完成，更新船舶状态
                if (isNeedUpdate)
                {
                    DataBusiness.SaveObjectList<Ship>(shipList);    //修理、改造完成，更新船舶状态
                }
            }

            d.Add("name", roleinfo.Name);
            d.Add("gold", roleinfo.Gold);
            d.Add("diamond", roleinfo.Diamond);
            d.Add("food", roleinfo.Food);
            d.Add("seaman", roleinfo.SeaMan);
            d.Add("workman", roleinfo.WorkMan);
            d.Add("Level", roleinfo.Level);
            d.Add("Exp", roleinfo.Exp);
            d.Add("Windy_dir", roleinfo.Windy_dir);
            d.Add("Windy_level", roleinfo.Windy_level);
            d.Add("Windy_remain_time", roleinfo.Windy_remain_time);
            d.Add("PopularSeaPointId", roleinfo.PopularSeaPointId);
            d.Add("PopularProduceId", roleinfo.PopularProduceId);
            d.Add("PopularProduceRate", roleinfo.PopularProduceRate);
            d.Add("PopularRemainTime", roleinfo.PopularRemainTime);
        }

        /// 数据库 对应 客户端
        /// <summary>
        /// 数据库 对应 客户端
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public Dictionary<string, object> ChangeToResponse(RoleData role)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            result.Add("name", role.Name);
            result.Add("gold", role.Gold);
            result.Add("food", role.Food);
            result.Add("seaman", role.SeaMan);
            result.Add("workman", role.WorkMan);
            result.Add("Level", role.Level);
            result.Add("Exp", role.Exp);
            result.Add("Windy_dir", role.Windy_dir);
            result.Add("Windy_level", role.Windy_level);
            result.Add("Windy_remain_time", role.Windy_remain_time);
            result.Add("PopularSeaPointId", role.PopularSeaPointId);
            result.Add("PopularProduceId", role.PopularProduceId);
            result.Add("PopularProduceRate", role.PopularProduceRate);
            result.Add("PopularRemainTime", role.PopularRemainTime);

            return result;
        }

        #endregion 数据操作
    }
}
